package com.xiefengf.rabbitmq.consumer.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiefengf
 * @version 1.0
 * @date 2024/1/11 23:28
 *
 *
 */
@Configuration
@Slf4j
public class RabbitConfig {

    /**
     * ------------------------------------批量处理消息--------方式一----------------------------
     */
    @Bean(name = "consumerBatchContainerFactory1")
    public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory1(
            SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        // 创建 SimpleRabbitListenerContainerFactory 对象
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        // <X> 额外添加批量消费的属性
        factory.setBatchListener(true);
        return factory;
    }

    /**
     * ------------------------------------批量处理消息--------方式二----------------------------
     */
    @Bean(name = "consumerBatchContainerFactory2")
    public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory2(
            SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        // 创建 SimpleRabbitListenerContainerFactory 对象
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        // 额外添加批量消费的属性
        factory.setBatchListener(true);
        // <X>
        // 设置批量大小为5
        factory.setBatchSize(3);
        // 15s
        factory.setReceiveTimeout(15 * 1000L);
        factory.setConsumerBatchEnabled(true);
        return factory;
    }

}
